package Repository;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import model.User;

public class UserRepository {

	static HashMap<String, User> userCredentials = new HashMap<String, User>();

	static {
		User c = new User();
		c.setId(1);
		c.setFirstName("John");
		c.setLastName("Stone");
		c.setCity("San Jose");
		c.setUserName("john");
		c.setPassword("john");
		c.setRole("Manager");
		ArrayList<String> a = new ArrayList<String>();
		a.add("Import Lead");
		a.add("Assign Lead");
		a.add("Qualify Lead");
		a.add("Approve Quote");
		a.add("Approve Contract");
		a.add("Finalize Contract");
		c.setUserActions(a);
		userCredentials.put("john", c);

		c = new User();
		c.setId(2);
		c.setCity("San Jose");
		c.setFirstName("Dave");
		c.setLastName("Johnson");
		c.setUserName("dave");
		c.setPassword("dave");
		c.setRole("SalesRep");
		a = new ArrayList<String>();
		a.add("Merge Lead");
		a.add("Prepare Lead");
		a.add("Import Opportunity");
		a.add("Prepare Opportunity");
		a.add("Produce Quote");
		a.add("Import Contract");
		a.add("Prepare Contract");

		c.setUserActions(a);
		userCredentials.put("dave", c);

		c = new User();
		c.setId(3);
		c.setCity("New York");
		c.setFirstName("George");
		c.setLastName("Fleming");
		c.setUserName("george");
		c.setPassword("george");
		c.setRole("SalesRep");
		a = new ArrayList<String>();
		a.add("Merge Lead");
		a.add("Prepare Lead");
		a.add("Import Opportunity");
		a.add("Prepare Opportunity");
		a.add("Produce Quote");
		a.add("Import Contract");
		a.add("Prepare Contract");
		c.setUserActions(a);
		userCredentials.put("george", c);

		c = new User();
		c.setId(4);
		c.setCity("San Jose");
		c.setFirstName("Elan");
		c.setLastName("Kramer");
		c.setUserName("elan");
		c.setPassword("elan");
		c.setRole("Administrator");
		a = new ArrayList<String>();
		a.add("Create Rule");
		a.add("Update Rule");
		a.add("Delete Rule");
		a.add("Create Account");
		a.add("Update Account");
		a.add("Delete Account");
		c.setUserActions(a);
		userCredentials.put("elan", c);

		c = new User();
		c.setId(5);
		c.setCity("San Jose");
		c.setFirstName("Joe");
		c.setLastName("Parker");
		c.setUserName("joe");
		c.setPassword("joe");
		c.setRole("Logistics Head");
		a = new ArrayList<String>();
		a.add("Approve Contract");
		c.setUserActions(a);
		userCredentials.put("joe", c);

		c = new User();
		c.setId(6);
		c.setCity("San Jose");
		c.setFirstName("Mark");
		c.setLastName("Thomas");
		c.setUserName("mark");
		c.setPassword("mark");
		c.setRole("Logistics Head");
		a = new ArrayList<String>();
		a.add("Approve Contract");
		c.setUserActions(a);
		userCredentials.put("mark", c);

		c = new User();
		c.setId(7);
		c.setCity("Chicago");
		c.setFirstName("Jeff");
		c.setLastName("Thomson");
		c.setUserName("jeff");
		c.setPassword("jeff");
		c.setRole("CEO");
		a = new ArrayList<String>();
		a.add("Approve Contract");
		c.setUserActions(a);
		userCredentials.put("jeff", c);

		c = new User();
		c.setId(8);
		c.setCity("Detroit");
		c.setFirstName("Amy");
		c.setLastName("Peterson");
		c.setUserName("amy");
		c.setPassword("amy");
		c.setRole("Delivery Head");
		a = new ArrayList<String>();
		a.add("Approve Contract");
		c.setUserActions(a);
		userCredentials.put("amy", c);

		c = new User();
		c.setId(9);
		c.setCity("Dallas");
		c.setFirstName("Nick");
		c.setLastName("Micheal");
		c.setUserName("nick");
		c.setPassword("nick");
		c.setRole("Delivery Rep");
		a = new ArrayList<String>();
		a.add("Approve Contract");
		c.setUserActions(a);
		userCredentials.put("nick", c);

		c = new User();
		c.setId(10);
		c.setCity("Detroit");
		c.setFirstName("Brian");
		c.setLastName("Peterson");
		c.setUserName("brian");
		c.setPassword("brian");
		c.setRole("Finance Head");
		a = new ArrayList<String>();
		a.add("Approve Contract");
		c.setUserActions(a);
		userCredentials.put("brian", c);
	}

	public static User getUser(String userName) {
		return userCredentials.get(userName.toLowerCase());

	}

	public static User getSalesRep(String cityName) {
		Collection<User> users = userCredentials.values();
		for (User u : users) {
			if (u.getCity().equalsIgnoreCase(cityName)
					&& u.getRole().equalsIgnoreCase("SalesRep")) {
				return u;
			}
		}

		return null;
	}

	public static List<User> getUserList() {
		Collection<User> users = userCredentials.values();
		List<User> approvers = new ArrayList();
		for (User u : users){
			if (!(u.getRole().equalsIgnoreCase("SalesRep") || u.getRole().equalsIgnoreCase("Administrator"))){
				approvers.add(u);
			}
		}
		return approvers;
	}

}
